package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Client;
import model.Users;

/**
 * Servlet implementation class Login
 * This servlet handles the user logging into the application
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}
	public void init() throws ServletException {
		// Do required initialization
		//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/config.properties"); 
	}

	/**
	 * The get method is currently not used.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * On a post, the Login servlet gets the name and password from the correct
	 * parameters. The Users class is then used to verify the password. If 
	 * correct, the user is redirected to the Flight Search Query page. If
	 * incorrect, the user is kept on the login page and the appropriate error
	 * message is displayed.
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		HttpSession session = request.getSession();
		
		//Get username/password
		String username=request.getParameter("name");
		String password=request.getParameter("password");

		String rememberMe = request.getParameter("remember_me");
		if(rememberMe != null && rememberMe.equals("on")){
			Cookie remember = new Cookie("remember", username);
			remember.setMaxAge(60*30);
			response.addCookie(remember);
		}

		//Try to verify the user/password combo. 
		Users user = new Users();
		boolean loggedIn = user.verifyPasswordDB(username, password);
		
		int userId = user.getUserID(username);
		
		Client client = user.getClient(userId);

		
		
		//If an error happened then display it otherwise redirect to the Flight
		//Search Query page.
		if(!loggedIn){
			request.setAttribute("loginError","Username/password incorrect");
			request.getRequestDispatcher("/Login.jsp").forward(request, response);
		} else {
			session.setAttribute("user", username);
			session.setAttribute("client", client);
			
			//setting session to expiry in 30 mins
			session.setMaxInactiveInterval(20*60);
			
			
			
			//response.sendRedirect("FlightSearchQuery.jsp");
			//response.encodeRedirectURL("FlightSearchQuery.jsp");
			response.sendRedirect(response.encodeRedirectURL("FlightSearchQuery.jsp"));
		}
	}

}